package com.demo2do.fms.entity.supplier.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.format.PersistentFormat;

/**
 * 服务供方基本信息
 * 
 * 与supplier关联关系:一对一
 * 
 * @author Wy
 *
 */
@Entity
@Table(name="FMS_SUPPLIER_INFO")
@DynamicUpdate
public class SupplierInfo {
    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
    @SequenceGenerator(name = "generator", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 1)
    private Long id;
    
    /** 与supplier关联 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @PersistentFormat
    private Supplier supplier;
    
    /** 运输公司曾用名（全称）I */
    private String formerName;
    
    /** 运输公司曾用名（全称）II */
    private String secondFormerName;
    
    /**
     * 公司性质 
     * 
     * 01--私营
     * 02--国有控股
     * 03--国有参股 
     * 04--中外合资
     * 05--上市公司
     * 枚举：CompanyProperty
     */
    private String companyProperty;
    
    /** 董事长 */
    private String chairman;
    
    /** 总经理  */
    private String manager;
    
    /** 总经理 EMAIL I */
    private String managerEmail;
    
    /** 总经理 EMAIL II */
    private String secondManagerEmail;
    
    /** 总经理联系电话  */
    private String managerPhone;

    /** 成立年/月 */
    @DateTimeFormat(pattern = "yyyy-MM")
    private Date yearFounded;
    
    /** 注册资金（万元） */
    private String registerCapital;
    
    /** 注册地  */
    private String registerAddress;
    
    /** 联系地址 */
    private String contactAddress;
    
    /** 体系标准文件类型 */
    private String standardDocument;
    
    /** 体系标准文件类型 - 有效期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date standardDocumentDate;
    
    /** 营业执照原件扫描件上传  */
    private String businessLicense;
    
    /** 营业执照原件扫描件 - 有效期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date businessLicenseDate;
    
    /** 质量体系认证证书原件扫描件上传  */
    private String qualitySystem;
    
    /** 质量体系认证证书原件扫描件 - 有效期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date qualitySystemDate;
    
    /** 办公及停车场地租房/购房合同扫描件上传 */
    private String leaseContract;
    
    /** 办公及停车场地租房/购房合同扫描件 - 有效期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date leaseContractDate;
    
    /** 道路运输许可证 */
    private String transportCert;
    
    /** 道路运输许可证 - 有效期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date transportCertDate;

    /**
     * 统一社会信用代码
     */
    private String uniformSocialCreditCode;

    /**
     * 供应商类型
     */
    private String supplierType;

    /**
     * 地点简称
     */
    private String shortLocationName;


    /**
     * 开户行
     */
    private String bankName;

    /**
     * 开户行城市
     */
    private String bankCity;

    /**
     * 银行账户
     */
    private String bankAccount;

    /**
     * 付款信息最后修改时间
     */
    private Date lastModifyPaymentInfoTime;

    /** 创建时间  */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    
    /** 最后修改时间  */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lastModifyTime;
    
    /** 创建者 */
    private Long creator;
    
    /** 修改者 */
    private Long lastOperator;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public SupplierInfo(){
    }
    
    /**
     * @param supplier
     * @param operator
     */
    public SupplierInfo(Supplier supplier, Long operator) {
        this.supplier = supplier;
        this.creator = operator;
        this.createTime = new Date();
        this.lastOperator = operator;
        this.lastModifyTime = new Date();
        this.disabled = false;
    }

    /**
     * 
     * @param version
     * @param operator
     * @return
     */
    public SupplierInfo change(SupplierModifiedVersion version, Long operator) {
        
        this.formerName = version.getFormerName();
        this.secondFormerName = version.getSecondFormerName();
        this.chairman = version.getChairman();
        this.manager = version.getManager();
        this.managerPhone = version.getManagerPhone();
        this.managerEmail = version.getManagerEmail();
        this.registerCapital = version.getRegisterCapital();
        this.yearFounded = version.getYearFounded();
        this.registerAddress = version.getRegisterAddress();
        this.contactAddress = version.getContactAddress();
        
        this.standardDocument = version.getStandardDocument();
        this.standardDocumentDate = version.getStandardDocumentDate();
        
        if (StringUtils.isNotBlank(version.getBusinessLicense())) {
            this.businessLicense = version.getBusinessLicense();
        }
        
        if (StringUtils.isNotBlank(version.getQualitySystem())) {
            this.qualitySystem = version.getQualitySystem();
        }
        
        if (StringUtils.isNotBlank(version.getLeaseContract())) {
            this.leaseContract = version.getLeaseContract();
        }
        
        if (StringUtils.isNotBlank(version.getTransportCert())) {
            this.transportCert = version.getTransportCert();
        }
        
        this.businessLicenseDate = version.getBusinessLicenseDate();
        this.qualitySystemDate = version.getQualitySystemDate();
        this.leaseContractDate = version.getLeaseContractDate();
        this.transportCertDate = version.getTransportCertDate();
        
        this.lastModifyTime = new Date();
        this.lastOperator = operator;

        if (StringUtils.isNotBlank(version.getUniformSocialCreditCode())){
            this.uniformSocialCreditCode = version.getUniformSocialCreditCode();
        }

        return this;
        
    }
    
    /**
     * @param operator
     * @return
     */
    public SupplierInfo disable(Long operator) {
        this.disabled = true;
        this.lastOperator = operator;
        this.lastModifyTime = new Date();
        return this;
    }
    
    /**
     * @param operator
     * @return
     */
    public SupplierInfo operate(Long operator) {
        this.lastOperator = operator;
        this.lastModifyTime = new Date();
        return this;
    }
    
    /**
     * 公司性质
     * 
     * @return
     */
    @Transient
    public String getCompanyDescription() {
    	CompanyProperty property = CompanyProperty.codeOf(this.companyProperty);
    	if (property != null) {
    		return property.getDescription();
    	}
        return null;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isValid() {
        return StringUtils.isNoneBlank(this.companyProperty) &&
               StringUtils.isNoneBlank(this.chairman) &&
               StringUtils.isNoneBlank(this.manager) &&
               StringUtils.isNoneBlank(this.managerEmail) &&
               StringUtils.isNoneBlank(this.managerPhone) &&
               (this.yearFounded != null) &&
               (this.registerCapital != null) &&
               StringUtils.isNoneBlank(this.registerAddress) &&
               StringUtils.isNoneBlank(this.contactAddress) &&
               StringUtils.isNoneBlank(this.businessLicense) &&
               StringUtils.isNoneBlank(this.qualitySystem) &&
               StringUtils.isNoneBlank(this.leaseContract) &&
               (this.disabled == false);
    }
    
    /**
     * @return
     */
    public String inValidMessage() {
        
        if (StringUtils.isBlank(this.companyProperty)) {
            return "无效公司性质";
        } else if (StringUtils.isBlank(this.chairman)) {
            return "董事长信息不存在";
        } else if (StringUtils.isBlank(this.manager) || StringUtils.isBlank(this.managerEmail) || StringUtils.isBlank(this.managerPhone)) {
            return "总经理信息不全";
        } else if (this.yearFounded == null) {
            return "成立年份信息不存在";
        } else if (this.registerCapital == null || StringUtils.isBlank(registerAddress)) {
            return "注册资金或注册地信息不全";
        } else if (StringUtils.isBlank(standardDocument)) {
            return "体系标准文件类型未填写";
        } else if (StringUtils.isBlank(this.contactAddress)) {
            return "联系地址信息不存在";
        } else if (StringUtils.isBlank(businessLicense)) {
            return "营业执照原件扫描件未上传";
        } else if (StringUtils.isBlank(leaseContract)) {
            return "办公及停车场地租房/购房合同扫描件未上传";
        } else if (this.disabled) {
            return "无效供方基本信息";
        } else {
            return null;
        }
        
    }
    
    /**
     * @return
     */
    @Transient
    public String getStandardDocumentString() {
        
        String[] documents = StringUtils.split(standardDocument, ",");
        List<String> values = new ArrayList<String>();
        
        if (ArrayUtils.isNotEmpty(documents)) {
        	
        	for (String document : documents) {
                StandardDocument standardDocument = StandardDocument.codeOf(document);
                values.add(standardDocument.getDescription());
            }
            
            return StringUtils.join(values, " , ");
        	
        }
        
        return null;
        
    }

    /**
     * 更新支付信息
     * @param supplierInfo
     * @return
     */
    public SupplierInfo updatePaymentInfo(SupplierInfo supplierInfo) {

        this.supplierType = supplierInfo.supplierType;
        this.shortLocationName = supplierInfo.shortLocationName;
        this.bankName = supplierInfo.bankName;
        this.bankAccount = supplierInfo.bankAccount;
        this.bankCity = supplierInfo.bankCity;
        this.lastModifyPaymentInfoTime = new Date();

        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the supplier
     */
    public Supplier getSupplier() {
        return supplier;
    }

    /**
     * @return the formerName
     */
    public String getFormerName() {
        return formerName;
    }

    /**
     * @return the secondFormerName
     */
    public String getSecondFormerName() {
        return secondFormerName;
    }

    /**
     * @return the companyProperty
     */
    public String getCompanyProperty() {
        return companyProperty;
    }

    /**
     * @return the chairman
     */
    public String getChairman() {
        return chairman;
    }

    /**
     * @return the manager
     */
    public String getManager() {
        return manager;
    }

    /**
     * @return the managerEmail
     */
    public String getManagerEmail() {
        return managerEmail;
    }

    /**
     * @return the secondManagerEmail
     */
    public String getSecondManagerEmail() {
        return secondManagerEmail;
    }

    /**
     * @return the managerPhone
     */
    public String getManagerPhone() {
        return managerPhone;
    }

    /**
     * @return
     */
    public Date getYearFounded() {
        return yearFounded;
    }

    /**
     * @return
     */
    public String getRegisterCapital() {
		return registerCapital;
	}

    /**
     * @return the registerAddress
     */
    public String getRegisterAddress() {
        return registerAddress;
    }

    /**
     * @return the contactAddress
     */
    public String getContactAddress() {
        return contactAddress;
    }

    /**
     * @return the businessLicense
     */
    public String getBusinessLicense() {
        return businessLicense;
    }

    /**
     * @return the businessLicenseDate
     */
    public Date getBusinessLicenseDate() {
        return businessLicenseDate;
    }

    /**
     * @return the qualitySystem
     */
    public String getQualitySystem() {
        return qualitySystem;
    }

    /**
     * @return the qualitySystemDate
     */
    public Date getQualitySystemDate() {
        return qualitySystemDate;
    }

    /**
     * @return the leaseContract
     */
    public String getLeaseContract() {
        return leaseContract;
    }

    /**
     * @return the leaseContractDate
     */
    public Date getLeaseContractDate() {
        return leaseContractDate;
    }

    /**
     * @return the transportCert
     */
    public String getTransportCert() {
        return transportCert;
    }
    
    /**
     * @return
     */
    public Date getTransportCertDate() {
        return transportCertDate;
    }

    /**
     * @return
     */
    public String getStandardDocument() {
        return standardDocument;
    }

    /**
     * @return the standardDocumentDate
     */
    public Date getStandardDocumentDate() {
        return standardDocumentDate;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the lastModifyTime
     */
    public Date getLastModifyTime() {
        return lastModifyTime;
    }

    /**
     * @return the creator
     */
    public Long getCreator() {
        return creator;
    }

    /**
     * @return the lastOperator
     */
    public Long getLastOperator() {
        return lastOperator;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param supplier the supplier to set
     */
    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    /**
     * @param formerName the formerName to set
     */
    public void setFormerName(String formerName) {
        this.formerName = formerName;
    }

    /**
     * @param secondFormerName the secondFormerName to set
     */
    public void setSecondFormerName(String secondFormerName) {
        this.secondFormerName = secondFormerName;
    }

    /**
     * @param companyProperty the companyProperty to set
     */
    public void setCompanyProperty(String companyProperty) {
        this.companyProperty = companyProperty;
    }

    /**
     * @param chairman the chairman to set
     */
    public void setChairman(String chairman) {
        this.chairman = chairman;
    }

    /**
     * @param manager the manager to set
     */
    public void setManager(String manager) {
        this.manager = manager;
    }

    /**
     * @param managerEmail the managerEmail to set
     */
    public void setManagerEmail(String managerEmail) {
        this.managerEmail = managerEmail;
    }

    /**
     * @param secondManagerEmail the secondManagerEmail to set
     */
    public void setSecondManagerEmail(String secondManagerEmail) {
        this.secondManagerEmail = secondManagerEmail;
    }

    /**
     * @param managerPhone the managerPhone to set
     */
    public void setManagerPhone(String managerPhone) {
        this.managerPhone = managerPhone;
    }

    /**
     * @param yearFounded
     */
    public void setYearFounded(Date yearFounded) {
        this.yearFounded = yearFounded;
    }

    /**
     * @param registerCapital
     */
    public void setRegisterCapital(String registerCapital) {
		this.registerCapital = registerCapital;
	}

    /**
     * @param registerAddress the registerAddress to set
     */
    public void setRegisterAddress(String registerAddress) {
        this.registerAddress = registerAddress;
    }

    /**
     * @param contactAddress the contactAddress to set
     */
    public void setContactAddress(String contactAddress) {
        this.contactAddress = contactAddress;
    }

    /**
     * @param businessLicense the businessLicense to set
     */
    public void setBusinessLicense(String businessLicense) {
        this.businessLicense = businessLicense;
    }

    /**
     * @param businessLicenseDate the businessLicenseDate to set
     */
    public void setBusinessLicenseDate(Date businessLicenseDate) {
        this.businessLicenseDate = businessLicenseDate;
    }

    /**
     * @param qualitySystem the qualitySystem to set
     */
    public void setQualitySystem(String qualitySystem) {
        this.qualitySystem = qualitySystem;
    }

    /**
     * @param qualitySystemDate the qualitySystemDate to set
     */
    public void setQualitySystemDate(Date qualitySystemDate) {
        this.qualitySystemDate = qualitySystemDate;
    }

    /**
     * @param leaseContract the leaseContract to set
     */
    public void setLeaseContract(String leaseContract) {
        this.leaseContract = leaseContract;
    }

    /**
     * @param leaseContractDate the leaseContractDate to set
     */
    public void setLeaseContractDate(Date leaseContractDate) {
        this.leaseContractDate = leaseContractDate;
    }

    /**
     * @param transportCert the transportCert to set
     */
    public void setTransportCert(String transportCert) {
        this.transportCert = transportCert;
    }
    
    /**
     * @param transportCertDate
     */
    public void setTransportCertDate(Date transportCertDate) {
        this.transportCertDate = transportCertDate;
    }

    /**
     * @param standardDocument
     */
    public void setStandardDocument(String standardDocument) {
        this.standardDocument = standardDocument;
    }

    /**
     * @param standardDocumentDate the standardDocumentDate to set
     */
    public void setStandardDocumentDate(Date standardDocumentDate) {
        this.standardDocumentDate = standardDocumentDate;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param lastModifyTime the lastModifyTime to set
     */
    public void setLastModifyTime(Date lastModifyTime) {
        this.lastModifyTime = lastModifyTime;
    }

    /**
     * @param creator the creator to set
     */
    public void setCreator(Long creator) {
        this.creator = creator;
    }

    /**
     * @param lastOperator the lastOperator to set
     */
    public void setLastOperator(Long lastOperator) {
        this.lastOperator = lastOperator;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public String getUniformSocialCreditCode() {
        return uniformSocialCreditCode;
    }

    public void setUniformSocialCreditCode(String uniformSocialCreditCode) {
        this.uniformSocialCreditCode = uniformSocialCreditCode;
    }

    public String getSupplierType() {
        return supplierType;
    }

    public void setSupplierType(String supplierType) {
        this.supplierType = supplierType;
    }

    public String getShortLocationName() {
        return shortLocationName;
    }

    public void setShortLocationName(String shortLocationName) {
        this.shortLocationName = shortLocationName;
    }
    public String getBankName() {
        return bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    public String getBankCity() {
        return bankCity;
    }

    public void setBankCity(String bankCity) {
        this.bankCity = bankCity;
    }

    public String getBankAccount() {
        return bankAccount;
    }

    public void setBankAccount(String bankAccount) {
        this.bankAccount = bankAccount;
    }


    public Date getLastModifyPaymentInfoTime() {
        return lastModifyPaymentInfoTime;
    }

    public void setLastModifyPaymentInfoTime(Date lastModifyPaymentInfoTime) {
        this.lastModifyPaymentInfoTime = lastModifyPaymentInfoTime;
    }
}